﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Storm.TextEditor.Languages;

namespace Storm.TextEditor.Test
{
	/// <summary>
	/// Represents the main Form of the application.
	/// </summary>
	public partial class MainForm
		: Form
	{
		#region Methods

		#region Private

		/// <summary>
		/// Creates a new file.
		/// </summary>
		private void New()
		{
			if (textEditor.Saved == false)
			{
				DialogResult dialogResult = MessageBox.Show("The current file is unsaved. Do you want to save it?", "Save File", 
					MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification, 
					false);

				if (dialogResult == DialogResult.Yes)
					this.Save();
			}

			textEditor.Text = "";
		}

		/// <summary>
		/// Opens a file.
		/// </summary>
		private void Open()
		{
			DialogResult dialogResult = openFileDialog.ShowDialog();
			if (dialogResult == DialogResult.OK)
				textEditor.Open(openFileDialog.FileName);
		}

		/// <summary>
		/// Saves the current file.
		/// </summary>
		private void Save()
		{
			if (textEditor.FileName != "")
				textEditor.Save();
			else
				this.SaveAs();
		}

		/// <summary>
		/// Saves the current file with a new name.
		/// </summary>
		private void SaveAs()
		{
			DialogResult dialogResult = saveFileDialog.ShowDialog();
			if (dialogResult == DialogResult.OK)
				textEditor.Save(saveFileDialog.FileName);
		}

		/// <summary>
		/// Exits this instance.
		/// </summary>
		private void Exit()
		{
			if (textEditor.Saved == false)
			{
				DialogResult dialogResult = MessageBox.Show("The current file is unsaved. Do you want to save it?", "Save File",
					MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification,
					false);

				if (dialogResult == DialogResult.Yes)
					this.Save();
			}

			this.Close();
		}

		#endregion

		#region EventHandlers

		private void OnNewClick(object sender, EventArgs e)
		{
			this.New();
		}

		private void OnOpenClick(object sender, EventArgs e)
		{
			this.Open();
		}

		private void OnSaveClick(object sender, EventArgs e)
		{
			this.Save();
		}

		private void OnSaveAsClick(object sender, EventArgs e)
		{
			this.SaveAs();
		}

		private void OnExitClick(object sender, EventArgs e)
		{
			this.Exit();
		}

		#endregion

		#endregion

		/// <summary>
		/// Initializes a new instance of the <see cref="MainForm"/> class.
		/// </summary>
		public MainForm()
		{
			InitializeComponent();

			string filter = "All Files (*.*)|*.*|" + LanguageLoader.DialogFilter;
			openFileDialog.Filter = filter;
			saveFileDialog.Filter = filter;
		}
	}
}
